
local bb = require("badboy")
bb.loadutilslib()
init("",1)
local width,height = getScreenSize()
setUIOrientation(0)


id = createHUD()     --创建一个HUD


if width/height == 750/1334 then
	setScreenScale(750,1334)    --在540*960分辨率的手机中开发了脚本，要在720*1280的设备中运行 -
else
	showHUD(id,"分辨率比例不同，不保证脚本完美运行",11,"0xffff0000","0xffffffff",0,100,0,228,32)      --显示HUD内容
end

init("0", 1); --以当前应用 Home 键在右边初始化
FD_MAIN_UI = {}
FD_TAP_POINT = {}
FD_FIND_POINT = {}

FD_MAIN_UI ={
	主界面={0,0,width,height,"0|0|0x4da8f3,1|-3|0x50a0f2,9|-7|0x4d9eef,12|-13|0x6591d5,13|-24|0xcfd4c9"},
	战斗界面={0, 0, width, height,"0|0|0xbaa153,-1|-8|0x27920a,11|-9|0xb89c52,17|-4|0x1759b5"},
	任务界面={0, 0, width, height,"0|0|0x0a111a,1|3|0xfafafa,4|9|0xf8f8f8,10|14|0xffffff,21|6|0x1b2128,44|22|0x1b2128,44|26|0xffffff,-47|13|0x81897f"},
	战斗胜利={0, 0, width, height,"0|0|0xceaf5e,1|7|0xccb36a,2|15|0xccb064,-13|17|0xecda7b,21|14|0xeacf65"},
	世界地图={0, 0, width, height,"0|0|0xb4a979,7|7|0x50786e,13|24|0x43a172,22|32|0x30394a,1171|650|0xbdbc7d,1181|649|0x14515a,1198|650|0xbfbf8c,1211|661|0x143f4c"}
}


local FD_TAP_POINT = {
	战斗界面={
		预设阵型={1147,51},
		最近使用1={1051,448},
		开战={1237,671}
	},
	战斗胜利={
		确定={874,678}
	},
	主界面={
		任务={77,696},
		大地图={185,684}
	},
	任务界面={
		领取={905,180},
		返回={49,32}
	},
	世界地图={
		返回={59,24}
	}
}

FD_FIND_POINT = {
	世界地图={
		英雄={0, 0, width, height,"0|0|0xffffff,0|-2|0xffffff,8|-10|0xfefefe,14|-7|0xfafafb,13|8|0xdedae4,13|18|0xc6c5d2,31|13|0xececf0,32|3|0xffffff"},
		任务={0, 0, width, height,"0|0|0x63fdff,1|-2|0x63fdff,0|-8|0x416781,0|-22|0x63fdff"},
		前往任务={0, 0, 749, 1333,"0|0|0xe3fcff,4|2|0x33bfba,4|9|0x40ad6c,26|38|0xaeb3ae,32|39|0xffffff,35|39|0x2d5d2e,59|40|0x206020,63|40|0xffffff"},
		前往劫车={1, 0, 1331, 748,"0|0|0x2e6c20,7|-3|0x2d6821,13|-4|0xffffff,20|-7|0x28681f,28|-6|0xf6f6f6,38|-9|0x2a6125"}
	},
	
	杂七杂八={
		确定={506, 640, 822, 742,"0|0|0xefeced,2|0|0x51577a,85|-2|0xd9dadb,96|-4|0xffffff,139|-1|0x0f495b"},
		攻击遗迹确定={296, 138, 1037, 548,"0|0|0xffffff,1|0|0xffffff,4|0|0xe0e1e6,13|-6|0x454a68,17|-1|0x558e65"},
		攻击确定={172, 81, 1153, 642,"0|0|0x7f81a6,8|-6|0x5f3224,31|-7|0xda744a,91|-10|0xf2f2f2,102|-10|0xb4afad"},
		领取奖励确定={147, 123, 1133, 720,"0|0|0xdfd8e7,10|-5|0xedecf3,21|2|0x225d2d,107|-2|0x899588,113|-2|0xffffff,152|11|0xfafafa,159|-9|0x185921"},
		英雄升级={610, 534, 1242, 736,"0|0|0x8c8376,2|1|0x6a460d,13|2|0x6a470d,9|10|0x665125,4|16|0xd8a017,-6|-42|0xfffcec"},
		运送回家={285, 142, 1043, 549,"0|0|0x85899d,10|-1|0x404564,13|13|0xffffff,23|11|0x4ab347,29|10|0xfcfcfc"},
		现在出发={264, 132, 1063, 550,"0|0|0x44bd4d,1|0|0x41a151,2|0|0x394b5c,5|0|0xffffff,15|-1|0xfdfefe,43|-3|0xededf0,44|4|0xb4b6c2"},
		恢复体力={318, 223, 1018, 561,"0|0|0xeeaa11,4|7|0xbb170b,28|1|0x52cb47,97|-12|0xc9ccc9,101|-14|0xffffff,126|-7|0x43c73c"},
		治疗伤势={318, 223, 1018, 561,"0|0|0x1f5f23,2|0|0xc5c8c5,5|0|0xb3b8b3,12|-1|0xacb2ac,18|-1|0x335d37,34|-1|0x2f5c34"},
		被打撤退={64, 61, 1272, 658,"0|0|0xffffff,0|-6|0x2c7b98,3|-6|0xffffff,6|-17|0x2494b6,25|-6|0x374261,29|-44|0xd0f6fe"},
		失误点到公会={0, 0, 312, 112,"0|0|0xaea079,11|6|0x7ba5a3,17|-1|0x27786f,38|-17|0x4c8792,92|-7|0x0b111b,98|-7|0xffffff,104|-4|0xfcfcfc,155|10|0xfdfdfd,157|11|0x252930"}
	}
}
FD_FIND_IMAGE = {
	世界地图={
		英雄="hero.png"
	}
}

FD_TEZHENG_POINT = {
	主界面={
		发现任务={
			{0, 0, width, height,"0|0|0x08c7ff,1|0|0x08c9ff,1|1|0x0fc6f0,1|2|0x12aede,1|3|0x12addd,1|4|0x12abdc"},
			{173, 69, 930, 617,"0|0|0x26568c,1|0|0x1d65a2,1|-1|0x108bcf,2|-1|0x1097d7"},
			{208, 70, 1133, 656,"0|0|0x26568c,1|0|0x1d65a2,1|-1|0x108bcf,2|-1|0x1097d7"},
			{0, 0, width, height,"0|0|0x4da6c8,1|0|0x517faa,1|-1|0x4abbdd,2|-1|0x5281ac,4|-4|0x578db5"}
		},
		驴拉车={
			{3, 70, 1331, 750,"0|0|0x6f503c,2|5|0x4c331f,4|9|0xffecc2,6|8|0xffffdc,-12|4|0x332818"},
			{3, 70, 1331, 750,"0|0|0x665041,5|5|0x221c1c,6|9|0x4b322f,9|10|0x8e511e"},
			{3, 70, 1331, 750,"0|0|0x6b5544,1|1|0x705044,8|3|0x291f11,11|6|0x743a13,18|1|0x4f433e"},
			{3, 70, 1331, 750,"0|0|0x63433b,8|-1|0x412f26,11|6|0xa46124"},
			{3, 70, 1331, 750,"0|0|0x473430,1|6|0x3d3530,1|10|0x7e4e33,8|11|0xfff3dd"}
		}
	},
	战斗界面={
		开战={
			{1017, 581, 1331, 754,"0|0|0xd5d4de,1|-1|0x424a68,10|6|0x524e68,2|36|0x226225"}
		}
	},
	任务界面={
		领取任务={
			{99, 96, 1009, 249,"0|0|0xa5b6a7,7|6|0xa6b5a9,8|13|0x416260,16|6|0x5e7961,25|8|0x9aac8e"}
		}
	},
	大地图界面={
		劫车={
			{0, 0, width, height,"0|0|0xfb0000,1|0|0x3d0606,1|1|0xf90000,0|1|0xf70000,2|0|0x4b4a4a"},
			{0, 0, width, height,"0|0|0xf60000,0|1|0xee0000,0|2|0xe70000,1|2|0x530000,1|3|0x380000"},
			{1, 1, width-1, height-1,"0|0|0xef0000,1|0|0x9c0000,12|1|0xdc0000,9|9|0xce0000,8|12|0x4b4b4b"},
			{1, 1, 1333, 750,"0|0|0x610000,2|-3|0xed0000,4|-3|0xed0000,6|-5|0xf60000"},
			{0, 0, 749, 1333,"0|0|0xf90000,4|-6|0x5b0000,11|-7|0xf20000,15|-7|0x4c0000,21|-4|0xeb0000,21|4|0xcf0000,9|9|0x4b4b4b"},
			{1, 0, 1331, 748,"0|0|0xf00000,6|0|0x3b0000,8|0|0x720000,15|0|0xdb0000,15|7|0x4b4b4b"}
		}
	}
}

function FD_FIND_TZ(tab)
	if tab == nil then
		return
	end
	
	for v,s in pairs(tab) do
		x, y = findColor({s[1], s[2], s[3], s[4]},s[5],95, 0, 0, 0)
		if x > -1 then
			return {x,y}
		end
	end
	
	return nil
end
--判断用户当前是否为静止
screenPoint = {}
function isUnMove()
	if screenPoint == nil then
		for i =0 ,10,1 do 
			local x = math.random(width)
			local y = math.random(height)
			setmetatable(screenPoint,{x,y,getColor(x,y)})     -- 把 mymetatable 设为 mytable 的元表 
		end
	else 
		local isMove = false
		for v ,s in pairs(screenPoint) do 
			if getColor(s[1],s[2]) ~= s[3] then
				return false;
			end
		end
		
		return true
	end
	
end

function isNeedSoldiers()
	x, y = findColor({0, 0, width, height}, 
		"0|0|0xffffff,-7|2|0x303812,-10|10|0xe7e9e2,-7|13|0x2a2a15",
		95, 0, 0, 0)
	if x > -1 then
		return true
	else
		return false
	end
end

function FD_C_UI()
	for name, value in pairs(FD_MAIN_UI) do
		x, y = findColor({value[1], value[2], value[3], value[4]},value[5],95,0,0,0)
		if x > -1 then
			return name;
		end
	end
	
	return nil
end

function FD_F_COLOR(tab)
	if tab == nil then
		return
	end
	
	x, y = findColor({tab[1], tab[2], tab[3], tab[4]},tab[5],93, 0, 0, 0)
	if x > -1 then
		return {x,y}
	end
	
	return nil
end

function FD_F_IMG(image)
	x, y = findImageInRegionFuzzy(image, 60, 0, 0, width, height, 0);
	if x ~= -1 and y ~= -1 then        --如果在指定区域找到某图片符合条件
		return {x,y}
	else                               --如果找不到符合条件的图片
		--sysLog("未找到符合条件的坐标！");
		return nil
	end
end

function FD_F_GO()
	point = findColors({0, 0, 749, 1333}, 
		"0|0|0x96ecfd,-1|0|0xadecf2,-1|1|0x89cffe,0|2|0x6f8eff,-2|2|0x7eb4ff",
		95, 0, 0, 0)
	if #point ~= 0 then
		return true
	else 
		return false
	end
end

function FD_Q_TAP(tab)
	if tab == nil then
		return
	end
	--id = createHUD()     --创建一个HUD
	--showHUD(id,"",12,"0x00ff0000","finger.png",0,tab[1],tab[2],40,48)     --变更显示的HUD内容
	touchDown(1, tab[1], tab[2])
	mSleep(500)
	touchUp(1, tab[1], tab[2])  
	--hideHUD(id)
end

local repeatCount = 0;
local taskEnd = false;
while true do
	mSleep(500)
	local isUnMove = isUnMove()
	
	if isUnMove then
		sysLog("-----")
	end
	
	C_UI = FD_C_UI() --当前窗口
	if C_UI == "主界面" then
		if FD_F_GO() == false then
			
			if repeatCount == 0 then
				touchDown(1, 200, 200)
				touchDown(2, width-200, 200)
				
				mSleep(200)
				touchMove(1, 300, 200)
				touchMove(2, width-300, 200)
				mSleep(500)
				
				touchUp(1, 200, 200)  
				touchUp(2, 200, 200)  
				
			end
			
			repeatCount =repeatCount + 1
			
			--如果自动任务结束就去劫车
			if taskEnd then
				TAP_POINT = FD_TAP_POINT.主界面.大地图
				FD_Q_TAP(TAP_POINT)
				mSleep(500)
				
				TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.大地图界面.劫车)
				FD_Q_TAP(TAP_POINT)
				mSleep(500);
				
			else
				TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.主界面.发现任务)
				if TAP_POINT == nil then          
					--先随机点几个地方
					
					
					TAP_POINT = FD_TAP_POINT.主界面.任务
					FD_Q_TAP(TAP_POINT)
					mSleep(500)
				else
					TAP_POINT = {TAP_POINT[1],TAP_POINT[2]+100}
					FD_Q_TAP(TAP_POINT)
					mSleep(500)
				end
			end
			
			
		end
	elseif C_UI == "任务界面" then
		TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.任务界面.领取任务)
		if TAP_POINT == nil then --任务完成，退出脚本
			pressHomeKey()  --返回桌面
			lua_exit();
			
			--自动任务结束 
			taskEnd = true
			TAP_POINT = FD_TAP_POINT.任务界面.返回
			FD_Q_TAP(TAP_POINT)
			mSleep(500)
			
		else
			FD_Q_TAP(TAP_POINT)
			mSleep(500)
		end
		
		
	elseif C_UI == "战斗界面" then
		TAP_POINT = FD_TAP_POINT.战斗界面.预设阵型
		FD_Q_TAP(TAP_POINT)
		mSleep(500)
		
		TAP_POINT = FD_TAP_POINT.战斗界面.最近使用1
		FD_Q_TAP(TAP_POINT)
		mSleep(500)
		
		TAP_POINT = FD_FIND_TZ(FD_TEZHENG_POINT.战斗界面.开战)
		FD_Q_TAP(TAP_POINT)
		mSleep(500)
	elseif C_UI == "战斗胜利" then
		TAP_POINT = FD_TAP_POINT.战斗胜利.确定
		FD_Q_TAP(TAP_POINT)
		mSleep(500)
	elseif C_UI == "世界地图" then
		
		if taskEnd then
			FD_GO_TASK_POINT = FD_F_COLOR(FD_FIND_POINT.世界地图.前往劫车)
			FD_Q_TAP(FD_GO_TASK_POINT)
			mSleep(500)
			
		else
			FD_GO_TASK_POINT = FD_F_COLOR(FD_FIND_POINT.世界地图.前往任务)
			if FD_GO_TASK_POINT ~= nil then
				FD_Q_TAP(FD_GO_TASK_POINT)
				mSleep(500)
			else
				FD_TASK_POINT = FD_F_COLOR(FD_FIND_POINT.世界地图.任务)
				FD_Q_TAP(FD_TASK_POINT)
				mSleep(500)
				
			end
		end
		
		
	else
		--杂七杂八的结算界面
		sysLog("杂七杂八的结算界面")
		for v,s in pairs(FD_FIND_POINT) do
			for v1,s1 in pairs(s) do
				TAP_POINT = FD_F_COLOR(s1)
				FD_Q_TAP(TAP_POINT)
			end
		end
	end
	
end

